package com.atguigu.spzx.manager.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.atguigu.spyx.model.common.com.atguigu.spyx.model.entity.product.Product;
import com.atguigu.spyx.model.common.com.atguigu.spyx.model.entity.product.ProductDetails;
import com.atguigu.spyx.model.common.com.atguigu.spyx.model.entity.product.ProductSku;
import com.atguigu.spyx.model.dto.system.ProductDto;
import com.atguigu.spyx.model.dto.system.ProductSaveDto;
import com.atguigu.spyx.model.vo.system.ProductVo;
import com.atguigu.spzx.manager.mapper.ProductDetailsMapper;
import com.atguigu.spzx.manager.mapper.ProductMapper;
import com.atguigu.spzx.manager.mapper.ProductSkuMapper;
import com.atguigu.spzx.manager.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author LT
 * @create 2023-11-10-19:34
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    ProductMapper productMapper;

    @Resource
    ProductDetailsMapper productDetailsMapper;

    @Resource
    ProductSkuMapper productSkuMapper;
    @Override
    public PageInfo<ProductVo> page(Integer pageNum, Integer pageSize, ProductDto productDto) {
        PageHelper.startPage(pageNum,pageSize);
        List<ProductVo> list = productMapper.findAll(productDto);
        PageInfo<ProductVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void save(ProductSaveDto productSaveDto) {
        Product product = new Product();
        BeanUtil.copyProperties(productSaveDto,product);
        productMapper.save(product);
        Long productId = product.getId();


        ProductDetails productDetails = new ProductDetails();
        productDetails.setId(productId);
        productDetails.setProductId(productId);

        productDetails.setImageUrls(productSaveDto.getDetailsImageUrls());
        productDetailsMapper.save(productDetails);

        List<ProductSku> productSkuList = productSaveDto.getProductSkuList();
        Snowflake snowflake = IdUtil.createSnowflake(1,1);
        productSkuList.forEach(productSku -> {
            productSku.setProductId(productId);
            productSku.setSkuCode(snowflake.nextIdStr());
            productSku.setSkuName(product.getName()+" "+productSku.getSkuSpec());
            productSku.setSaleNum(0);
            productSku.setStatus(0);
        });
        productSkuMapper.batchSave(productSkuList);

    }

    @Override
    public ProductSaveDto getById(Long id) {

        ProductSaveDto productSaveDto = productMapper.getById(id);
        return productSaveDto;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateById(ProductSaveDto productSaveDto) {
        Product product = new Product();
        BeanUtil.copyProperties(productSaveDto,product);
        productMapper.updateById(product);

        ProductDetails productDetails = new ProductDetails();
        productDetails.setId(product.getId());

        productDetails.setImageUrls(productSaveDto.getDetailsImageUrls());
        productDetailsMapper.updateById(productDetails);
        List<ProductSku> skuList = productSaveDto.getProductSkuList();
        skuList.forEach(productSku -> {
            productSku.setSkuName(product.getName()+" "+
                    productSku.getSkuSpec());
            productSkuMapper.updateById(productSku);
        });

    }

    @Override
    public void deleteById(Long id) {
        productMapper.deleteById(id);
        productSkuMapper.deleteByProductId(id);
        productDetailsMapper.deleteById(id);
    }

    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        productMapper.updateAuditStatus(id, auditStatus);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        productMapper.updateStatus(id,status);
        productSkuMapper.updateStatus(id,status);
    }
}
